﻿using System.Reflection;
using Red.Controls.Diagrams.Declarators;
using Red.Controls.ViewModels;

namespace Red.Toolkit.NodeDeclaratorEditor
{
    public class DummyDropPropertyDataItem : DataItem
    {
        #region Data

        private readonly NodeDropDeclarator _drop;
        private readonly PropertyInfo _propertyInfo;
        private readonly bool _isInherited;

        #endregion Data

        #region C-tor

        public DummyDropPropertyDataItem( DataItem parent, NodeDropDeclarator drop, string propertyName, bool isInherited )
            : base( parent )
        {
            _drop = drop;
            _propertyInfo = _drop.GetType().GetProperty( propertyName );
            Title = propertyName;
            IsReadOnly = isInherited;
            _isInherited = isInherited;
            IsExpanded = true;
        }

        #endregion C-tor

        #region DataItem implementation

        public override string Type => "String";

        public override string Value
        {
            get
            {
                var value = _propertyInfo.GetValue( _drop );
                return value?.ToString() ?? "";
            }
            set
            {
                _propertyInfo.SetValue( _drop, value );
            }
        }

        public override DataItemKind Kind => DataItemKind.Simple;

        #endregion DataItem implementation
    }
}
